<template>
  <el-card>
    <el-form label-width="100px">
      <el-form-item label="SPU名称">
        <el-input placeholder="请你输入SPU名称"></el-input>
      </el-form-item>
      <el-form-item label="SPU品牌">
        <el-select v-model="SpuParams.tmId">
          <el-option :label="tradeMark.tmName" :value="tradeMark.id" v-for="tradeMark in MYAllTradeMark" :key="tradeMark.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="SPU描述">
        <el-input type="textarea" placeholder="请你输入SPU描述" v-model="SpuParams.description"></el-input>
      </el-form-item>
      <el-form-item label="SPU图片">
        <el-upload @:file-list="imgList" before-upload="handlerUpload" action="/api/admin/product/fileUpload" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
          <el-icon>
            <Plus />
          </el-icon>
        </el-upload>
        <el-dialog v-model="dialogVisible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="SPU销售属性" size="normal">
        <!-- 展示销售属性的下拉菜单 -->
        <el-select>
          <el-option label="华为"></el-option>
          <el-option label="oppo"></el-option>
          <el-option label="vivo"></el-option>
        </el-select>
        <el-button style="margin-left: 10px" type="primary" size="default" icon="Plus">
          添加属性
        </el-button>
        <!-- table展示销售属性与属性值的地方 -->
        <el-table border style="margin: 10px 0px" :data="saleAttr">
          <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
          <el-table-column label="销售属性名字" width="120px" prop="saleAttrName"></el-table-column>
          <el-table-column label="销售属性值">
            <!-- row:即为当前SPU已有的销售属性对象 -->
            <template #="{ row, $index }">
              <el-tag class="mx-1" closable style="margin: 0px 5px" @close="row.spuSaleAttrValueList.splice(index, 1)" v-for="(item, index) in row.spuSaleAttrValueList" :key="row.id">
                {{ item.saleAttrValueName }}
              </el-tag>
              <el-button type="primary" size="small" icon="Plus"></el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120px">
            <template #="{ row, $index }">
              <el-button type="primary" size="small" icon="Delete" @click="saleAttr.splice($index, 1)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default">保存</el-button>
        <el-button type="primary" size="default" @click="cancel(0)">取消</el-button>
      </el-form-item>
    </el-form>

  </el-card>
</template>

<script setup lang="ts">
import { reqAllSaleAttr, reqAllTradeMark, reqSpuHasSaleAttr, reqSpuImageList } from '@/api/product/spu'
import type { AllTradeMark, HasSaleAttrResponseData, SaleAttrResponseData, SpuData, SpuHasImg } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'
let all = defineProps(['all'])
const $emit = defineEmits(['changeScene'])
//取消按钮的回调
const cancel = (status: number) => {
  $emit('changeScene', status)
}
let MYAllTradeMark = ref()
let imgList = ref()
let saleAttr = ref()
let allSaleAttr = ref()

//存储已有的SPU对象
// 这是数据的对象.
let SpuParams = ref<SpuData>({
  category3Id: '', //收集三级分类的ID
  spuName: '', //SPU的名字
  description: '', //SPU的描述
  tmId: '', //品牌的ID
  spuImageList: [],
  spuSaleAttrList: []
})
//照片钱上传成功之前的钩子约束文件的大小与类型
const handlerUpload = (file: any) => {
  if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/gif') {
    if (file.size / 1024 / 1024 < 3) {
      return true
    } else {
      ElMessage({
        type: 'error',
        message: '上传文件务必小于3M'
      })
      return false
    }
  } else {
    ElMessage({
      type: 'error',
      message: '上传文件务必PNG|JPG|GIF'
    })
    return false
  }
}

//控制对话框的显示与隐藏
let dialogVisible = ref<boolean>(false)
//存储预览图片地址
let dialogImageUrl = ref<string>('')
//照片墙点击预览按钮的时候触发的钩子
const handlePictureCardPreview = (file: any) => {
  dialogImageUrl.value = file.url
  //对话框弹出来
  dialogVisible.value = true
}
const initHashspuData = async (spu: SpuData) => {
  SpuParams.value = spu
  //spu:即为父组件传递过来的已有的SPU对象[不完整]
  //获取全部品牌的数据
  let result: AllTradeMark = await reqAllTradeMark()

  //获取某一个品牌旗下全部售卖商品的图片
  let result1: SpuHasImg = await reqSpuImageList(spu.id as number)
  //获取已有的SPU销售属性的数据
  let result2: SaleAttrResponseData = await reqSpuHasSaleAttr(spu.id as number)
  //获取整个项目全部SPU的销售属性
  let result3: HasSaleAttrResponseData = await reqAllSaleAttr()
  //存储全部品牌的数据
  MYAllTradeMark.value = result.data
  //SPU对应商品图片
  imgList.value = result1.data.map(item => {
    return {
      name: item.imgName,
      url: item.imgUrl
    }
  })
  //存储已有的SPU的销售属性
  saleAttr.value = result2.data
  //存储全部的销售属性
  allSaleAttr.value = result3.data
  console.log(result)
  console.log(result1)
  console.log(result2)
  console.log(result3)
}

defineExpose({ initHashspuData })
</script>

<style scoped>
</style>